package com.rules.admin.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Map;

public class JSONUtils {
   public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
   private static final String MES = "无法读取json内容";
   private static final Logger LOGGER;

   public static String write(Object object) {
      if (object != null) {
         try {
            return OBJECT_MAPPER.writeValueAsString(object);
         } catch (JsonProcessingException var2) {
            LOGGER.error("JSON字符串转换出错", var2);
         }
      }

      return "";
   }

   public static <T> T read(String str) {
      StringBuilder builder = new StringBuilder("{");
      builder.append("\"root\"").append(":").append(str).append("}");
      JavaType javaType = OBJECT_MAPPER.constructType(Map.class);

      try {
         Map<String, Object> objectMap = (Map)OBJECT_MAPPER.readValue(builder.toString(), javaType);
         return (T) objectMap.get("root");
      } catch (IOException var4) {
         LOGGER.error("无法读取json内容", var4);
         return null;
      }
   }

   public static <T> T read(String str, Class<T> clazz) {
      JavaType javaType = OBJECT_MAPPER.constructType(clazz);

      try {
         return OBJECT_MAPPER.readValue(str, javaType);
      } catch (IOException var4) {
         LOGGER.error("无法读取json内容", var4);
         return null;
      }
   }

   public static <T> T readObject(byte[] data, Class<T> clazz) {
      JavaType javaType = OBJECT_MAPPER.constructType(clazz);

      try {
         return OBJECT_MAPPER.readValue(data, javaType);
      } catch (IOException var4) {
         LOGGER.error("无法读取json内容", var4);
         return null;
      }
   }

   public static <T> T readObject(InputStream data, Class<T> clazz) {
      JavaType javaType = OBJECT_MAPPER.constructType(clazz);

      try {
         return OBJECT_MAPPER.readValue(data, javaType);
      } catch (IOException var4) {
         LOGGER.error("无法读取json内容", var4);
         return null;
      }
   }

   public static <T> T readObject(String jsonStr, Class<T> clazz) {
      return JSON.parseObject(jsonStr, clazz);
   }

   static {
      OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      OBJECT_MAPPER.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
      OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
      OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
      LOGGER = LoggerFactory.getLogger(JSONUtils.class);
   }
}
